package com.flight.trans.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.flight.trans.R;
import com.flight.trans.bean.RouterBean.RouterData;
import com.flight.trans.bean.RouterBean.RouterData.RouterChildren;

import java.util.List;

/**
 * 抽屉填充
 */
public class MainDrawableAdapter extends BaseExpandableListAdapter {

    private List<RouterData> navigationGroup;
    private LayoutInflater mInflater ;
    private Context context ;

    public MainDrawableAdapter(Context context, List<RouterData> navigationGroup){
        this.context = context ;
        this.navigationGroup = navigationGroup;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return navigationGroup.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(navigationGroup == null)
            return 0 ;

        if(navigationGroup.get(groupPosition) == null)
            return 0 ;

        if(navigationGroup.get(groupPosition).getChildren() == null)
            return 0 ;

        return navigationGroup.get(groupPosition).getChildren().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        if(navigationGroup == null)
            return 0 ;

        return navigationGroup.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return navigationGroup.get(groupPosition).getChildren().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = mInflater.inflate(R.layout.adapter_navigation_group,parent,false);

        TextView mTvGroupName = convertView.findViewById(R.id.mTvGroupName);
        mTvGroupName.setText(navigationGroup.get(groupPosition).getMeta().getTitle());

        mTvGroupName.setTextColor(context.getResources().getColor(isExpanded ? R.color.white : R.color.black));
        convertView.setBackgroundColor(context.getResources().getColor(isExpanded ? R.color.color_blue : R.color.white));

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        RouterChildren childBean = navigationGroup.get(groupPosition).getChildren().get(childPosition);

        if (convertView == null)
            convertView = mInflater.inflate(R.layout.adapter_navigation_item,parent,false);

        TextView mTvItemName = convertView.findViewById(R.id.mTvItemName);

        mTvItemName.setText(childBean.getMeta().getTitle());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null)
                    listener.onClick(groupPosition ,childPosition);
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    private OnItemClickListener listener ;

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener ;
    }

    public interface OnItemClickListener {
        void onClick(int groupPosition,int position);
    }
}
